var sksMap = window.sksMap = sksMap || {};

(function(){
	var sks = sks||{};
	(function(){
	sks.dom = sks.dom || {};
	sks.browser = sks.browser ||{};
	sks.browser.ie=sks.ie=/msie(\d+\.\d+)/i.test(navigator.userAgent)?(document.documentMode||+RegExp['\x241']):undefined;
	
	sks.dom._NAME_ATTRS=(function(){
		var result={
				'cellpadding':'cellpadding',
				'cellspacing':'cellspacing',
				'colspan':'colspan',
				'rowspan':'rowspan',
				'valign':'vAlign',
				'usemap':'useMap',
				'frameborder':'frameBorder'
		};
		if(sks.browser.ie < 8){
			result['for']='hmtlFor';
			result['class']='className';
		}else{
			result['htmlFor']='for';
			result['className']='class';
		}
		return result;
	})();
	
	sks.lang = sks.lang || {};
	sks.lang.Event = function (type, target) {
        this.type = type;
        this.returnValue = true;
        this.target = target || null;
        this.currentTarget = null;
    };
	
    sks.object = sks.object || {};
    sks.extend =
    sks.object.extend = function (target, source) {
        for (var p in source) {
            if (source.hasOwnProperty(p)) {
                target[p] = source[p];
            }
        }

        return target;
    };
    
    sks.string = sks.string ||{};
    sks.string.toCamelCase = function(source){
    	//提前判断，提高getStyle等的效率 
    	if(source.indexOf('-'<0 && source.indexOf('_')<0)){
    		return source;
    	}
    	return source.replace(/[-_][^-_]/g,function(match){
    		return match.charAt(1).toUpperCase();
    	});
    };
    
	sks.dom.get=function(id){
		if('string'== typeof id || id instanceof String){
			return document.getElementById(id);
			//document or element return itself
		}else if(id && id.nodeName && (id.nodeType == 1 || id.nodeType==9)){
			return id;
		}
		return null;
	};
	sks.dom._styleFixer = sks.dom._styleFixer || {};
	sks.dom._styleFilter = sks.dom._styleFilter || [];
    sks.dom._styleFilter.filter = function (key, value, method) {
        for (var i = 0, filters = sks.dom._styleFilter, filter; filter = filters[i]; i++) {
            if (filter = filter[method]) {
                value = filter(key, value);
            }
        }
        return value;
    };
    
    sks.dom.setStyle = function(element,key,value){
    	var dom = sks.dom,fixer;
    	// 放弃了对firefox 0.9的opacity的支持
    	element = dom.get(element);
    	key = sks.string.toCamelCase(key);
    	if(fixer = dom._styleFilter){
    		value = fixer.filter(key,value,'set');
    	}
    	fixer = dom._styleFixer[key];
    	(fixer && fixer.set)?fixer.set(element,value):(element.style[fixer||key]=value);
    	
    	return element;
    	
    };
    
    sks.setStyle = sks.dom.setStyle;
    
    sks.dom.setStyles = function(element,styles){
    	element = sks.dom.get(element);
    	for(var key in styles){
    		sks.dom.setStyle(element,key,styles[key]);
    	}
    	return element;
    };
    
	sks.dom.setAttr=function(element,key,value){
		element = sks.dom.get(element);
		if('style'==key){
			element.style.cssText = value;
		}else{
			key = sks.dom._NAME_ATTRS[key]||key;
			element.setAttribute(key,value);
		}
		return element;
	};
	
	sks.dom.setAttrs=function(element,attributes){
		element = sks.dom.get(element);
		for(var key in attributes){
			sks.dom.setAttr(element,key,attributes[key]);
		}
		return element;
	};
	
	sks.dom.create=function(tagName,opt_attributes){
		var element = document.createElement(tagName),
		    attributes = opt_attributes||{};
		    return sks.dom.setAttrs(element,attributes);
	};
	
})(); 
	
	var ExtInfoWindow = sksMap.ExtInfoWindow =function(map,content,opts){
			this._map = map;
			this._content = content||"";
			this._isOpen = false;
			
			var defOpts ={
				width:"650px",
				height:"382px",
				infowindowClass:"infoWindow",
				background:"url('/map/resource/infowindow/tipbox.gif') no-repeat center top"
					
			};	
		        

	        this._opts = opts = opts || {};
	        this._opts.infowindowClass = opts.infowindowClass || "infoWindow";
	        this._opts.windowStyle = opts.windowStyle || {};
	        this._opts.closeIconMargin = opts.closeIconMargin || "6px";
	        this._opts.closeIconUrl = opts.closeIconUrl || "close.png";
	        
	        
			this._opts = opts = opts||{};
			this._opts.width = opts.windowStyle.width||defOpts.width;
			this._opts.height = opts.windowStyle.width||defOpts.height;
			this._opts.infowindowClass = opts.infowindowClass||defOpts.infowindowClass;
			this._opts.windowStyle.background = opts.windowStyle.background||defOpts.background;
		};
	
	ExtInfoWindow.prototype = new BMap.Overlay();
	
	ExtInfoWindow.prototype.initialize = function(map){
		this._map = map;
		var div = this._div = sks.dom.create("div",{"class":this._opts.infowindowClass});
		sks.dom.setStyles(div, this._opts.windowStyle); 
		 
		div.style.position = "absolute" ;
		div.style.top = "50%";
		div.style.left = "50%";
		div.style.marginTop = "100px";
		div.style.marginLeft = "200px";
		
		this._setContent(this._content);
		//map.getPanes().markerPane.appendChild(child);
		var floatPane = map.getPanes().floatPane;
		floatPane.style.width = "auto";
		floatPane.appendChild(div);
		this._div = div;
		 
		return div;
	};
	
	ExtInfoWindow.prototype.draw = function(){
		var position = this._map.pointToOverlayPixel();
	};
	ExtInfoWindow.prototype.open = function(anchor){
		/*if(this._div){
			this.div.style.display="";
		}
		*/
		var me = this,poi;
		if(!this._isOpen) {
            this._map.addOverlay(this);
            this._isOpen = true;
            setTimeout(function(){
            //	me._dispatchEvent(me,"open",{"point":me._point});
            	var event = new sks.lang.Event("onopen");
            	event["point"] = me._point;
            	me.dispatchEvent(event);
            },10);
		}
		map._infowindow = me;
		this.show();
	};
	ExtInfoWindow.prototype.close = function(){
		/*
		if(this._div){
			this.div.style.display = "none";
		}
		*/
		var me = this;
		if(this._isOpen){
			this._map.removeOverlay(this);
			this._isOpen = false;
			map._infowindow = null;
			this.hide();
		}
	};
	
	sks.object.extend(ExtInfoWindow.prototype,{
		/**
		 * 获取关闭按钮的html
		 */
		_getColoseIcon : function(){
			var img = "<img src='/map/resource/infowindow/"+ this._opts.closeIconUrl +"' align='right' style='position:absolute;right:0px;cursor:pointer;margin:"+ this._opts.closeIconMargin +"' onclick='map._infowindow.close();'/>";
	        return img;
		},
		_setContent : function(content){
			if(!this._div){
				return;
			}
			var closeHtml = this._getColoseIcon();
			if(typeof content.nodeType==="undefined"){
				this._div.innerHTML = closeHtml + content;
			}else{
				this._div.innerHTML = closeHtml;
				this._div.appendChild(content); 
			}
			this._content = content;
			//TODO
			//添加click关闭infowindow
		}
		
	});
	

	
	
})();
	